package com.hss.springboot3.jpa;

import com.hss.springboot3.bean.UserEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 可以看到抛出的异常TranscationRequiredException，
 * 意思就是你当前的操作给你抛出了需要事务异常，
 * SpringDataJPA自定义SQL时需要在对应的接口或者调用接口的地方添加事务注解
 * @Transactional，来开启事务自动化管理。
 */
@Transactional
public interface UserJPA extends JpaRepository<UserEntity,Long> {

    @Query(value = "select * from user where age > ?1",nativeQuery = true)
    public List<UserEntity> ageLarge(int age);


    //根据用户名、密码删除一条数据
    @Modifying
    @Query(value = "delete from user where username = ?1 and pwd = ?2",nativeQuery = true)
    public void deleteQuery(String name,String pwd);

}
